//
//  MKAnnotoationExampleCalloutView.m
//  UILbrary
//
//  Created by mac on 17/3/9.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "MKAnnotoationExampleCalloutView.h"
#import "ExampleCalloutView.h"
#import "MKCustomAnnotation.h"
#define ANIMATION_DURATION 0.25
@interface MKAnnotoationExampleCalloutView()
/** 自定义calloutVeiw */
@property (nonatomic, strong) ExampleCalloutView *calloutVeiw;
@property (nonatomic, strong) MKPointAnnotation *pointAnnotation;
@end
@implementation MKAnnotoationExampleCalloutView


- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {

        self.canShowCallout = false;

        self.pointAnnotation = annotation;
    }
    return self;
}

- (void)prepareForReuse{
    [super prepareForReuse];
    [self.calloutVeiw removeFromSuperview];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    UIView *hitView = [super hitTest:point withEvent:event];
    
    if (CGRectContainsPoint(self.calloutVeiw.frame, point)) {
        return self.calloutVeiw;
    }
    return hitView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    if (selected) {
        
        [self.calloutVeiw removeFromSuperview];
        [self addSubview:self.calloutVeiw];
        self.calloutVeiw.frame = CGRectMake(self.frame.size.width / 2.0 - 100, -120, 200, 100);
        if (animated) {
            self.calloutVeiw.alpha = 0;
            [UIView animateWithDuration:ANIMATION_DURATION animations:^{
                self.calloutVeiw.alpha = 1;
            }];
        }
    }else{
        if (animated) {
            [UIView animateWithDuration:ANIMATION_DURATION animations:^{
                [self.calloutVeiw removeFromSuperview];
            }];
        }else{
            [self.calloutVeiw removeFromSuperview];
        }
    }
}


#pragma mark - lazy methods
- (ExampleCalloutView *)calloutVeiw{
    if (!_calloutVeiw) {
        _calloutVeiw = [[ExampleCalloutView alloc] initWithAnnotation:self.pointAnnotation];
    }
    return _calloutVeiw;
}
@end
